<?php
/**
 *
 * User: chenzheng
 * Date: 2023/5/8
 * Time: 16:05
 */

namespace app\api\model;


use app\api\help\Http;
use app\common\model\TimeModel;
use app\api\library\order\createOrder;
use think\facade\Db;

class MallWechat extends TimeModel
{
    
    /**
     * 获取access_token
     */
    public static function getAccessToken()
    {
        $where  = [['id', '=', 1]];
        $wechat = Db::name('mall_wechat')->where($where)->find();
        if ( !empty($wechat)) {
            if ($wechat[ 'expire_time' ] - time() < (7200 - 600)) {
                //access_token失效,重新获取access_token
                $res = self::initAccessToken($wechat);
                if ( !empty($res) && !isset($res[ 'errcode' ])) {
                    $data = [
                        'access_token' => $res[ 'access_token' ],
                        'expire_time'  => time() + $res[ 'expires_in' ],
                    ];
                    if (false !== Db::name('mall_wechat')->where($where)->update($data)) {
                        return $res[ 'access_token' ];
                    } else {
                        _outPut(204);
                    }
                } else {
                    // 获取access_token失败
                    _outPut(601, $res, 'wechat get access_token error');
                }
            } else {
                return $wechat[ 'access_token' ];
            }
        } else {
            _outPut(602, [], 'wechat_id is not exist');
        }
    }
    
    /**
     * 获取access_token
     */
    public static function initAccessToken($param)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$param['app_id']}&secret={$param['app_secret']}";
        $res = Http::request($url);
        $res = json_decode($res, true);
        return $res;
    }
    /**
     * 获取jsapi_ticket
     */
    public static function getJsapiTiket($wechat_id)
    {
        $where  = [['id', '=', 1]];
        $wechat = Db::name('mall_wechat')->where($where)->find();
        if ( !empty($wechat)) {
            if ($wechat[ 'jsapi_expire_time' ] - time() < 600) {
                //ticket失效,重新获取ticket
                $res = self::initJsapiTicket($wechat);
                if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) {
                    $data = [
                        'jsapi_ticket'      => $res[ 'ticket' ],
                        'jsapi_expire_time' => time() + $res[ 'expires_in' ],
                    ];
                    if (false !== Db::name('mall_wechat')->where($where)->update($data)) {
                        return $res[ 'ticket' ];
                    } else {
                        _outPut(204);
                    }
                } else {
                    // 获取ticket失败
                    _outPut(601, $res, 'wechat get ticket error');
                }
            } else {
                return $wechat[ 'jsapi_ticket' ];
            }
        } else {
            _outPut(602, $wechat_id, 'wechat_id is not exist');
        }
    }
    /**
     * 获取jsapi_ticket
     */
    public static function initJsapiTicket($param)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$param['access_token']}&type=jsapi";
        $res = Http::request($url);
        $res = json_decode($res, true);
        return $res;
    }
}